package com.hancher.gamelife2.weather;

import android.content.res.AssetManager;

import com.hancher.common.base.BaseApplication;
import com.hancher.common.javautil.EmptyUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;

/**
 * 描述 : 天气城市编码 <br/>
 * 时间 : 2023/12/28 14:15 <br/>
 * 作者 : Hancher ytu_shaoweijie@163.com
 */
public class WeatherCityCodeUtil {
    /**
     * 读取4级定位城市列表
     */
    static String CITY_CODE_EXCEL_NEW_ASSET_PATH4 = "weather/City4PositionInfo.csv";

    /**
     * 读取定位
     *
     * @return
     * @throws IOException
     */
    public static CityCodeBean queryCityCode4(String p, String c, String lo, String la) throws IOException {
        AssetManager assetManager = BaseApplication.getInstance().getAssets();
        InputStream in = assetManager.open(CITY_CODE_EXCEL_NEW_ASSET_PATH4);
        InputStreamReader isr1 = new InputStreamReader(in);
        BufferedReader isr = new BufferedReader(isr1);

        String[] line;
        isr.readLine();
        String strLine;
        CityCodeBean nearestCity = new CityCodeBean();
        BigDecimal loB = new BigDecimal(lo);
        BigDecimal laB = new BigDecimal(la);
        while (true) {
            strLine = isr.readLine();
            if (EmptyUtil.isEmpty(strLine)) {
                break;
            }
            line = strLine.split(",");
            CityCodeBean tmpCity = new CityCodeBean()
                    .setCityCode(line[0])
                    .setDistrict(line[2])
                    .setProvince(line[7])
                    .setCity(line[9])
                    .setLatitude(line[11])
                    .setLongitude(line[12]);
            if (line.length > 14) {
                tmpCity.setSubDistrict(line[14]);
            }
            if (tmpCity.getCity().equals(c) || tmpCity.getCity().equals(c + "市")) {
                tmpCity.setAccurate(2);
            } else if (tmpCity.getProvince().equals(p) || tmpCity.getProvince().equals(p + "省")) {
                tmpCity.setAccurate(1);
            }
            if (tmpCity.getAccurate() > nearestCity.getAccurate()) {
                nearestCity = tmpCity;
            } else if (tmpCity.getAccurate() == nearestCity.getAccurate()) {
                if (EmptyUtil.allNotEmpty(tmpCity.getLatitude(), tmpCity.getLongitude(),
                        nearestCity.getLatitude(), nearestCity.getLongitude())) {
                    BigDecimal yd = laB.subtract(new BigDecimal(tmpCity.getLatitude())).abs();
                    BigDecimal xd = loB.subtract(new BigDecimal(tmpCity.getLongitude())).abs();
                    tmpCity.setDistance(yd.multiply(yd).add(xd.multiply(xd)));
                    if (nearestCity.getDistance() == null) {
                        BigDecimal yd2 = laB.subtract(new BigDecimal(nearestCity.getLatitude())).abs();
                        BigDecimal xd2 = loB.subtract(new BigDecimal(nearestCity.getLongitude())).abs();
                        nearestCity.setDistance(yd2.multiply(yd2).add(xd2.multiply(xd2)));
                    }
                    if (tmpCity.getDistance().compareTo(nearestCity.getDistance()) < 0) {
                        nearestCity = tmpCity;
                    }
                }
            }
        }
        isr.close();
        return nearestCity;
    }
}
